package cn.com.ege.mvc.action.business;

import java.io.Serializable;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.ege.mvc.controller.BaseAction;
import cn.com.ege.mvc.controller.IAction;
import cn.com.ege.mvc.exception.BusinessException;
import cn.com.ege.mvc.model.business.ModelRule;
import cn.com.ege.mvc.model.business.OrderList;
import cn.com.ege.mvc.model.business.OrderShoe;
import cn.com.ege.mvc.model.business.Shoe;
import cn.com.ege.mvc.model.view.OrderShoeView;
import cn.com.ege.mvc.service.impl.business.ModelRuleService;
import cn.com.ege.mvc.service.impl.business.OrderListService;
import cn.com.ege.mvc.service.impl.business.OrderShoeService;
import cn.com.ege.mvc.service.impl.business.ShoeService;
import cn.com.ege.mvc.service.impl.view.OrderShoeViewService;
import cn.com.ege.mvc.util.PageController;

public class OrderShoeAction extends BaseAction implements IAction,
		Serializable {
	private static final long serialVersionUID = 8299975587235537983L;
	private final static Log logger = LogFactory.getLog(OrderListAction.class);
	private PageController pager = null;
	private OrderShoeService orderShoeService = null;
	private OrderShoe orderShoe = null;
	private OrderShoeViewService orderShoeViewService = null;
	private OrderShoeView orderShoeView = null;

	public String execute(HttpServletRequest request,
			HttpServletResponse response, ServletConfig servletConfig) {
		String act = request.getParameter("act");
		String order_id = request.getParameter("order_id");
		if (null != order_id && !"".equals(order_id)) {
			request.setAttribute("order_id", order_id);
		}
		if (null != act) {
			// 取列表
			if ("list".equals(act)) {
				OrderShoeView orderShoeView = new OrderShoeView();
				orderShoeView.setOrder_id(Long.parseLong(order_id));
				int totalRows = orderShoeViewService
						.getRecordCount(orderShoeView);
				String URL = request.getRequestURI();
				this.pager.setURL(URL);
				this.pager.setTotalRowsAmount(totalRows);
				List<OrderShoeView> resourceList = orderShoeViewService.list(
						new OrderShoeView(), this.pager.getPageStartRow(),
						pager.getPageSize(), "and order_id=" + order_id + " order by id desc", true);
				this.pager.setData(resourceList);
				OrderList orderList = new OrderList();
				orderList.setId(Long.parseLong(order_id));
				OrderListService orderListService = new OrderListService();
				orderList = orderListService.get(orderList);
				request.setAttribute("orderList", orderList);
				return getReturnURL(LIST);
			}
			// 点了添加或者点了修改
			else if ("get".equals(act)) {
				orderShoe = orderShoeService.get(orderShoe);
				ShoeService shoeService = new ShoeService();
				List<Shoe> shoeList = shoeService.listAll(new Shoe());
				request.setAttribute("shoeList", shoeList);
				ModelRuleService modelRuleService = new ModelRuleService();
				List<ModelRule> modelRuleList = modelRuleService
						.listAll(new ModelRule());
				request.setAttribute("modelRuleList", modelRuleList);
				return getReturnURL(EDIT);
			}
			// 保存表单
			else if ("save".equals(act)) {
				try {
					if (null != request.getParameter("status")) {
						orderShoe = orderShoeService.get(orderShoe);
						orderShoe.setTransact_status(1L);
					}
					orderShoeService.save(orderShoe);
					return getReturnURL("getList");
				} catch (BusinessException e) {
					// 保存原来表单已输入的内容
					request.setAttribute("orderShoe", orderShoe);
					addMessage(e.getMessage());
					return getReturnURL(EDIT);
				}
			}
			// 删除
			else if ("del".equals(act)) {
				String[] ids = request.getParameterValues("id");
				for (String id : ids) {
					OrderShoe orderShoe = new OrderShoe();// CommUser user =
					// new CommUser();
					if (null != id && !("".equals(id))) {
						orderShoe.setId(Long.parseLong(id));
						orderShoeService.delete(orderShoe);
					}
				}
				return getReturnURL("getList");
			}
		}
		addMessage("参数不正确!! act=" + act);
		return getReturnURL(ERROR);
	}

	public PageController getPager() {
		return pager;
	}

	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public OrderShoe getOrderShoe() {
		return this.orderShoe;
	}

	public void setOrderShoe(OrderShoe orderShoe) {
		this.orderShoe = orderShoe;
	}

	public void setOrderShoeService(OrderShoeService orderShoeService) {
		this.orderShoeService = orderShoeService;
	}

	public OrderShoeViewService getOrderShoeViewService() {
		return orderShoeViewService;
	}

	public void setOrderShoeViewService(
			OrderShoeViewService orderShoeViewService) {
		this.orderShoeViewService = orderShoeViewService;
	}

	public OrderShoeService getOrderShoeService() {
		return orderShoeService;
	}

	public OrderShoeView getOrderShoeView() {
		return orderShoeView;
	}

	public void setOrderShoeView(OrderShoeView orderShoeView) {
		this.orderShoeView = orderShoeView;
	}

}
